package main.flex.control
{
	import flash.utils.getQualifiedClassName;
	
	import main.flex.business.MainBusinessDelegate;
	import main.flex.business.responder.ConnectUserReponder;
	import main.flex.business.responder.GetMediaResponder;
	import main.flex.business.responder.SessionInfoReponder;
	import main.flex.conf.FacadeConf;
	import main.flex.model.CalendarModel;
	import main.flex.model.MainModel;
	import main.flex.model.MediaModel;
	import main.flex.model.VisioModel;
	
	import mx.rpc.remoting.RemoteObject;
	
	import org.servebox.foundry.control.AbstractController;
	import org.servebox.foundry.model.ModelLocator;

	public class MainController extends AbstractController
	{
		//--------------------------------------------------
		// Singleton implementation
		private static var _mainInstance 				: MainController;
		
		public static const MAIN_MODEL_ID				: String = "mainModel";
		
		private var _service 							: RemoteObject;
		
		public static function getInstance() : MainController
		{
			if ( _mainInstance == null )
			{
				_mainInstance = new MainController();
			}
			return _mainInstance;
		}		
 
		//----------------------------------------------------
		// Overriding methods
		override public function initializeModels():void
		{
			// The model can now be referenced from anywhere, using the ModelLocator singleton.
			ModelLocator.getInstance().registerModel( MAIN_MODEL_ID , new MainModel() );
			ModelLocator.getInstance().registerModel( MediaController.MEDIA_MODEL_ID , new MediaModel() );
			ModelLocator.getInstance().registerModel( VisioController.VISIO_MODEL_ID , new VisioModel() );
			ModelLocator.getInstance().registerModel( CalendarController.CALENDAR_MODEL_ID , new CalendarModel() );
		}
		
		// initialisation du service (rO.source == classe PHP)
		override public function initializeDelegates():void
		{
			var rO : RemoteObject = new RemoteObject( );
			rO.destination = String( FacadeConf.AMFPHP_FACADE );
			rO.source = "ConnexionService";
			
			_service = rO;
			
			registerBusinessDelegate( 	new MainBusinessDelegate( rO ),
			                   			getQualifiedClassName( MainBusinessDelegate ) 
			        );
		}
		
		override public function initializeSubControllers( ) : void
		{
			super.createSubController( MediaController, getQualifiedClassName( MediaController ) );
			super.createSubController( VisioController, getQualifiedClassName( VisioController ) );
		}
		
		// méthodes d'appels publiques à AMF
		// le nom de la méthode doit mapper sur celle du service PHP --> 'getSessionInfo'
		// un responder personnalisé (extends MainBaseResponder) doit être créé --> 'SessionInfoResponder'
		public function getSessionInfo( ) : void
		{
			getMainBusinessDelegate().getSessionInfo( new SessionInfoReponder() );
		}
		
		public function connectUser( login : String, mdp : String ) : void
		{
			getMainBusinessDelegate().connectUser( new ConnectUserReponder(), login, mdp );
		}
		
		public function getMedia( espaceId : int ) : void
		{
			//getMainBusinessDelegate().getMedia( new GetMediaResponder(), espaceId );
			getMainBusinessDelegate().getMedia( new GetMediaResponder(), espaceId );
		}
		
		// mécanisme interne de la classe : appel privé au BusinessDelegate
		public function getMainBusinessDelegate() : MainBusinessDelegate
		{
			return MainBusinessDelegate( getBusinessDelegate( getQualifiedClassName( MainBusinessDelegate ) ) );
		}
	}
}